﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using JiaBao.Core;

namespace JiaBao.WPF.Framework.SN
{
    public class RegisterManager
    {
        #region CONST

        private string filename = "JiaBao.Presentation.dll";

        #endregion

        private bool _isTest;
        public bool IsTest
        {
            get => _isTest;
            set => _isTest = value; 
        }
        public RegisterManager()
        {
            IsTest = false;
        }

        #region 方法 

        /// <summary>
        /// 读取并解析配置文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public virtual Register LoadSettings(string filePath = null)
        {
            var key = GetKey();

            string sn;
            if (IsTest)
                sn = DateTime.Now.ToFileTimeUtc().ToString();
            else
                sn = GetSN(key);

            var local = "";
            if (String.IsNullOrEmpty(filePath))
                filePath = Path.Combine(CommonHelper.MapPath(""), filename);


            if (File.Exists(filePath))
            {
                var text = File.ReadAllText(filePath);
                local = text;
            }
            else
            {
                if (IsTest)
                {
                    local = DateTime.Now.AddDays(7).ToFileTimeUtc().ToString();
                    SaveSettings(new Register() { Key = key, Sn = sn, LocalSn = local, IsTest = IsTest });
                }
            }
            return new Register() { Key = key, Sn = sn, LocalSn = local, IsTest = IsTest };

        }

        /// <summary>
        /// 保存配置到文件
        /// </summary>
        /// <param name="settings"></param>
        public virtual void SaveSettings(Register settings)
        {
            if (settings == null)
                throw new ArgumentNullException("settings");

            Directory.CreateDirectory(CommonHelper.MapPath(""));

            var filePath = Path.Combine(CommonHelper.MapPath(""), filename);
            if (!File.Exists(filePath))
                using (File.Create(filePath)) { }

            File.WriteAllText(filePath, settings.LocalSn);

        }

        #endregion


        #region 内部方法 

        private string GetKey()
        {
            //创建ManagementObjectSearcher对象
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            String strHardDiskID = null;//存储磁盘序列号
            //调用ManagementObjectSearcher类的Get方法取得硬盘序列号
            foreach (ManagementObject mo in searcher.Get())
            {
                strHardDiskID = mo["SerialNumber"].ToString().Trim();//记录获得的磁盘序列号
                break;
            }
            return strHardDiskID;//显示硬盘序列号
        }

        public string GetSN(string str)
        {
            StringBuilder sbTemp = new StringBuilder();
            //生成注册码
            for (int i = 0; i < str.Length; i++)
            {
                int tIn = str[i] % 10;
                string tCh = tIn.ToString();
                sbTemp.Append(tCh);
            }
            string strResult = sbTemp.ToString();
            return strResult;
        }



        #endregion
    }
}
